/**
 * Created by Administrator on 2018/8/17.
 */
InitCertusJqNumLimit();
// 资产库可用资产信息
var purchaseProductDatas = new Array();
$$scope.submitter = {};
var purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}
$$scope.submmitData = {};
$$scope.submmitData.purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}

var bqArray = {};
var oldBqArray = {};

$$scope.select = {};
// 采购记录
var proc1Columns = [{
    id: 'commodityCateName',
    title: '类目',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'commodityClassName',
    title: '类别',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'name',
    title: '名称',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'descAndSpec',
    title: '品名及规格',
    width: 200,
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'quantity',
    title: '数量',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'estimatedAmount',
    title: '预计金额',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
      }
}, {
    id: 'remark',
    title: '备注',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'operation',
    title: '操作',
    width: 60,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-success" onclick="showNowQuote(\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  查看</button>';
        content += '  ';
        return content;
    }
}];


function showNowQuote(biCode) {
    if (biCode) {;
        belowGrid.exhibitDatas = bqArray[biCode];
        belowGrid.refresh(false);

        ;
        endGrid.exhibitDatas = oldBqArray[biCode]
        endGrid.refresh(false);


        $(".hdlog").show();
        $(".hdgbuy").show();
    }
}

function showAllQuote() {
    var tempArray = new Array();
    if (bqArray) {
        for (var j in bqArray) {
            tempArray = tempArray.concat(bqArray[j]);
        }
    }
    belowGrid.exhibitDatas = tempArray;
    belowGrid.refresh(false);

    $(".hdgbuy").show();
}

// 供应商报价列表
var prcListColumns = [
    //     {
    //     id: 'no',
    //     title: '序号',
    //     width: 50,
    //     type: 'string',
    //     columnClass: 'text-center',
    // }, 
    {
        id: 'name',
        title: '名称',
        width: 74,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'supplierName',
        title: '供应商名称',
        // width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'supplierPhone',
        title: '联系方式',
        width: 110,
        type: 'string',
        columnClass: 'text-center',
    },
    {
        id: 'specification',
        title: '规格(参数)',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'price',
        title: '价格(说明)',
        // width: 120,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'needPrintName',
        title: '印章',
        // width: 120,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'priceIncludingTax',
        title: '价格(含税)',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'dateOfDelivery',
        title: '交货期',
        width: 80,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'guaranteeTime',
        title: '质保期',
        width: 74,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'paymentMethod',
        title: '付款方式',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'strengthsAndWeaknesses',
        title: '优劣势',
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'operation',
        title: '操作',
        width: 130,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-primary" onclick="openAgreementDownloadList(\'' + record.bqCode + '\',\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  协议</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-primary" onclick="openDownloadList(\'' + record.bqCode + '\',\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  附件</button>';
            content += '  ';
            return content;
        }
    }
];



function shupload(bqCode, biCode) {
    $(".hdupload").show();
    $$scope.agreementAaaa = {};
    $$scope.agreementAaaa.bqCode = bqCode;
    $$scope.agreementAaaa.biCode = biCode;
}

function openAgreementDownloadList(bqCode, biCode) {

    if (biCode && bqCode) {
        var nowBq = bqArray[biCode];
        if (nowBq) {
            for (var i in nowBq) {
                if (nowBq[i].supplierFile && nowBq[i].bqCode == bqCode) {
                    $$scope.modalContent = eval(nowBq[i].agreementFile)
                }
            }
        }
    }
    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {});
}

//打开 下载 列表
function openDownloadList(bqCode, biCode) {

    if (biCode && bqCode) {
        var nowBq = bqArray[biCode];
        if (nowBq) {
            for (var i in nowBq) {
                if (nowBq[i].supplierFile && nowBq[i].bqCode == bqCode) {
                    $$scope.modalContent = eval(nowBq[i].supplierFile);
                }
            }

        }
    }
    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {});
}


function hdgs() {
    $(".hdgs").hide();
}

// 结果清单
var endColumns = [
    //     {
    //     id: 'number',
    //     title: '序号',
    //     width: 45,
    //     type: 'string',
    //     columnClass: 'text-center',
    // }, 
    {
        id: 'commodityCateName',
        title: '资产类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'commodityClassName',
        title: '资产类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'assetId',
        title: '资产编号',
        width: 170,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'descAndSpec',
        title: '资产描述',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'status',
        title: '状态',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
        resolution: function () {
            return "已经入库";
        }
    }, {
        id: 'belongto',
        title: '归属人',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'belongProj',
        title: '归属项目',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }
];
var belowGrid;
var aboveGrid;
var endGrid;
var brCode;
$CertusFormAjax.ajax("queryByBrcode", {
    "brCode": nowBrCode
}, function (res) {
    if (res.data) {
        $$scope.headerMap = res.data.headerMap;
        brCode = res.data.headerMap.brCode;
        // purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;
        $$scope.submmitData.purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;
        $$scope.submmitData.purchaseStepEntity.projId = res.data.headerMap.projId;
        var datas = res.data.buyings;
        var priceOption1 = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '采购单条目信息',
            datas: datas,
            columns: proc1Columns,
            extraWidth: 20,
            gridContainer: 'priceContainer',
        };
        aboveGrid = $.fn.dlshouwen.grid.init(priceOption1);
        aboveGrid.load();

        //初始化 报价 文件 数据
        if (datas) {
            for (var i in datas) {
                if (datas[i].biCode) {
                    bqArray[datas[i].biCode] = datas[i].buyingQuoteItemEntities;
                }
            }
            //初始化 历史报价 文件 数据
            for (var i in datas) {
                if (datas[i].biCode) {
                    oldBqArray[datas[i].biCode] = datas[i].inventorQuoteItemEntities;
                }
            }
        }

        var prcListOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '资产信息',
            datas: [],
            columns: prcListColumns,
            gridContainer: 'supplier02Container',
            tools: '',
            pageSize: 10,
            extraWidth: 20,
            pageSizeLimit: [10, 15, 20]
        };
        belowGrid = $.fn.dlshouwen.grid.init(prcListOption);
        belowGrid.load();

        var endOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '结果清单',
            datas: [],
            columns: endColumns,
            gridContainer: 'endContainer',
            // toolbarContainer: 'endToolBarContainer',
            tools: '',
            pageSize: 10,
            extraWidth: 20,
            pageSizeLimit: [10, 15, 20]
        };
        endGrid = $.fn.dlshouwen.grid.init(endOption);
        endGrid.load();
    }
})


//文件列表下载
function downLoad(id) {
    var fileID = $(id).parent().parent().find(".fileDownloadPath").val();
    $CertusFormAjax.download("downloadFile", "fileId=" + fileID);
}